package com.rs.game.player.content.commands;

import org.Vote.MainLoader;
import org.Vote.VoteReward;

import com.rs.Settings;
import com.rs.cache.loaders.ItemDefinitions;
import com.rs.game.BodyGlow;
import com.rs.game.World;
import com.rs.game.player.Player;
import com.rs.game.player.actions.DonatorActions;
import com.rs.game.player.actions.HomeTeleport;
import com.rs.game.player.content.Commands;
import com.rs.game.player.content.ForumRights;
import com.rs.game.player.content.TicketSystem;
import com.rs.utils.Utils;

public class NormalPlayer {
	/*
	 * Handles Normal Player Commands
	 */
	public static boolean processCommand(final Player player, String[] cmd, boolean console, boolean clientCommand) {
		if (clientCommand) {

		} else {
			String message;

			switch (cmd[0]) {
			
			case "canpvp":
				player.setCanPvp(true);
				return true; 
				
			case "onlinetime":
				player.getPackets().sendGameMessage("You have been playing for " + "<col=008000>" +player.time+ "</col>"+" hours.");
				return true;

			case "glow":
				player.setNextBodyGlow(BodyGlow.generateRandomBodyGlow(100));
				return true;
				
			case "switchitemslook":
				if (player.isOldItemsLook()) {
					player.switchItemsLook();
				} else if (!player.isOldItemsLook()) {
					player.switchItemsLook();
				}
				return true;
			
			case "item":
				if (cmd.length < 2) {
					player.getPackets().sendGameMessage("Use: ::item itemId (option:amount)");
					return true;
				}
				try {
					int itemId = Integer.valueOf(cmd[1]);
					ItemDefinitions defs = ItemDefinitions.getItemDefinitions(itemId);
					if (defs.isLended()) {
						return true;
					}
					if (defs.isOverSized()) {
						player.getPackets().sendGameMessage("The item appears to be oversized.");
						return true;
					}
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::item itemId (optional:amount)");
				}
				return true;
			/*
			 * Set your account pin
			 * 
			 * Usage: ::setpin pin
			 * Example: ::setpin 1234
			 */
				
			case "setpin":
				try {
					if (!player.accountPinSet) {
						if (Integer.parseInt(cmd[1]) >= 1000 && Integer.parseInt(cmd[1]) <= 9999) {
							player.accountPinSet = true;
							player.setAccountPin(Integer.parseInt(cmd[1]));
							player.getPackets().sendGameMessage("<col=FF0000>You have set your account pin to '" + cmd[1] + "'.");
						} else {
							player.getPackets().sendGameMessage("You must set a 4 digit pin");
						}
					}
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Invalid pin number!");
				}
				return true;
			
			/*
			 * Make the user a donator or extreme donator if they donated
			 * 
			 * Usage: ::donated
			 * Example: ::donated
			 */
			case "donated":
				ForumRights.setRights(player);
				return true;

			/*
			 * Send a yell across the server
			 * 
			 * Usage: ::yell message goes here
			 * Example: ::yell this is a yell
			 */
            
			case "yell":
				message = "";
				for (int i = 1; i < cmd.length; i++) {
					message += cmd[i] + ((i == cmd.length - 1) ? "" : " ");
				}
				Commands.sendYell(player, Utils.fixChatMessage(message), false);
				return true;
				
			/*
			 * Teleport the player home
			 * 
			 * Usage: ::home
			 * Example: ::home
			 */
			case "home":
				player.getActionManager().setAction(new HomeTeleport(HomeTeleport.REAL_HOME_TELEPORT));
				return true;
				
			/*
			 * Check if you voted
			 * 
			 * Usage: ::check OR ::voted OR ::claim
			 * Example: ::check OR ::voted OR ::claim
			 */
			case "check":
			case "voted":
			case "claim":
				try {
					VoteReward reward = MainLoader.hasVoted(player.getUsername().toLowerCase().replaceAll(" ", "_"));
					if (reward != null) {
						switch (reward.getReward()) {
						case 0:
							player.getInventory().addItem(995, 5000000);
							break;
						case 1:
							player.getInventory().addItem(989, 1);
							break;
						case 2:
							player.getInventory().addItem(18831, 5);
							break;
						case 3:
							player.getInventory().addItem(15332, 3);
							break;
						case 4:
							player.getInventory().addItem(24155, 1);
							break;
						default:
							player.getPackets().sendGameMessage("Reward not found.");
							break;
						}
						player.setVotePoints(player.getVotePoints() + 5);
						player.getPackets().sendGameMessage("You recieve " + "<col=FF0000>"+5+"</col>" + " vote points for voting! Thank you.");
						player.getPackets().sendGameMessage("You currently have " + "<col=FF0000>"+player.getVotePoints()+"</col>" + " vote points.");
						World.sendWorldMessage("<img=7><col=FF0000>News: " + Utils.formatPlayerNameForDisplay(player.getDisplayName()) + " has just claimed their vote reward. Type ::vote to vote now!</col>", false);
					} else {
						player.getPackets().sendGameMessage("You have no items waiting for you.");
					}
				} catch (Exception e) {
					player.getPackets().sendGameMessage("An error has occured.");
				}
				return true;
				
			/*
			 * Empty the players inventory
			 * 
			 * Usage: ::clearinv
			 * Example: ::clearinv
			 */
			case "clearinv":
				player.getInventory().reset();
				return true;
				
			/*
			 * Change your password
			 * 
			 * Usage: ::changepass newpasshere
			 * Example: ::changepass mynewpassword
			 */
			case "changepass":
				message = "";
				for (int i = 1; i < cmd.length; i++) {
					message += cmd[i] + ((i == cmd.length - 1) ? "" : " ");
				}
				if (message.length() > 15 || message.length() < 5) {
					player.getPackets().sendGameMessage("Your password must be between 5-14 characters.");
					return true;
				}
				player.getPackets().sendGameMessage("You changed your password to \"" + message + "\".");
				player.setPassword(message);
				return true;
				
			/*
			 * Tell the player how many players are online
			 * 
			 * Usage: ::players
			 * Example: ::players
			 */
			case "players":
				player.getInterfaceManager().sendInterface(275);
				int number = 0;
				for (int i = 0; i < 310; i++) {
					player.getPackets().sendIComponentText(275, i, "");
				}
				for (Player players : World.getPlayers()) {
					if (players == null) {
						continue;
					}
					number++;
					String titles = "";
					if (players.getRights() == 0) {
						titles = "[Player]";
					}
					if (players.isSupporter()) {
						titles = "[<img=12><col=6666C9>Server Support</col>]";
					}
					if (players.isRespected()) {
						titles = "[<img=2><col=6666C9>Respected Member</col>]";
					}
					if (players.isGraphicDesigner()) {
						titles = "[<img=9><col=6666FF>Graphic Designer</col>]";
					}
					if (players.isDonator() && !players.isExtremeDonator()) {
						titles = "[<img=5><col=FF0000>Donator</col>]";
					}
					if (players.isExtremeDonator()) {
						titles = "[<img=4><col=02B41D>Extreme Donator</col>]";
					}
					if (players.getRights() == 1 && players.isStaff()) {
						titles = "[<img=0><col=008B00>Mod</col>]";
					}
					if (players.getRights() == 2 && players.isStaff()) {
						titles = "[<img=1><col=ff0000>Administrator</col>]";
					}
					if (players.getUsername().trim().equalsIgnoreCase("Corleone")) {
						titles = "[<img=1><col=FF8000><shad=000000>Owner</shad></col>]";
					}
					player.getPackets().sendIComponentText(275, (13 + number), titles + "" + players.getDisplayName());
				}
				player.getPackets().sendIComponentText(275, 1, "Phoenix Players");
				player.getPackets().sendIComponentText(275, 10, " ");
				player.getPackets().sendIComponentText(275, 11, "Who's Online");
				player.getPackets().sendIComponentText(275, 12, "There are current <col=FF0000>" + World.getPlayers().size() + "</col> players online.");
				player.getPackets().sendGameMessage("There are currently <col=FF0000>" + World.getPlayers().size() + "</col> players online.");
				return true;
				
			/*
			 * Checks amount of Penguins found
			 * 
			 *  Usage: ::penguins
			 *  Example ::penguins
			 */
			case "penguins":
			player.getPackets().sendGameMessage("You have found a total of " +player.gettotalpeng()+"/7 penguins.");
				return true;
			
			/*
			 * Set your yell color
			 * 
			 * Usage: ::yellcolor
			 * Example: ::yellcolor
			 */
			case "yellcolor":
				if (!player.isExtremeDonator()) {
					player.getPackets().sendGameMessage("You must be an extreme donator to use this feature.");
					return true;
				}
				player.getPackets().sendRunScript(109, new Object[] { "Please enter the yell color in HEX format." });
				player.getTemporaryAttributes().put("yellcolor", Boolean.TRUE);
				return true;
			/*
			 * Opens a ticket
			 * 
			 * Usage: ::ticket
			 * Example: ::ticket
			 */
			case "ticket":
				if (player.getMuted() > Utils.currentTimeMillis()) {
					player.getPackets().sendGameMessage("You're temporarily muted. Recheck in 48 hours.");
					return true;
				}
				TicketSystem.requestTicket(player);
				return true;
				
			/*
			 * Teleport to the donator zone if donator
			 * 
			 * Usage: ::donatorzone
			 * Example: ::donatorzone
			 */
			case "donatorzone":
			case "dz":
				DonatorActions.checkDonator(player);
				break;
				
			/*
			 * Opens a URL to the vote page
			 * 
			 * Usage: ::vote
			 * Example: ::vote
			 */
			case "vote":
				player.getPackets().sendOpenURL(Settings.VOTE_LINK);
				break;
				
			/*
			 * Opens a URL to the donation page
			 * 
			 * Usage: ::donate
			 * Example: ::donate
			 */
			case "donate":
				player.getPackets().sendOpenURL(Settings.DONATE_LINK);
				break;

			/*
			 * Opens an interface with all the commands
			 * 
			 * Usage: ::commands OR ::cmd
			 * Example: ::commands OR ::cmd
			 */
			case "cmd":
			case "commands":
				player.getInterfaceManager().sendInterface(275);
				for (int i = 0; i < 310; i++) {
					player.getPackets().sendIComponentText(275, i, "");
				}
				player.getPackets().sendIComponentText(275, 1, "Phoenix Commands");
				
				player.getPackets().sendIComponentText(275, 11, "Normal Player Commands");
				player.getPackets().sendIComponentText(275, 12, "----------------------------------------");
				player.getPackets().sendIComponentText(275, 13, "::players");
				player.getPackets().sendIComponentText(275, 14, "::changepass");
				player.getPackets().sendIComponentText(275, 15, "::clearinv");
				player.getPackets().sendIComponentText(275, 16, "::voted");
				player.getPackets().sendIComponentText(275, 17, "::home");
				player.getPackets().sendIComponentText(275, 18, "::ticket");
				player.getPackets().sendIComponentText(275, 19, "::donated");
				player.getPackets().sendIComponentText(275, 20, "----------------------------------------");
				
				player.getPackets().sendIComponentText(275, 22, "Donator Commands");
				player.getPackets().sendIComponentText(275, 23, "----------------------------------------");
				player.getPackets().sendIComponentText(275, 24, "::yell");
				player.getPackets().sendIComponentText(275, 25, "::donatorzone");
				player.getPackets().sendIComponentText(275, 26, "----------------------------------------");
				
				player.getPackets().sendIComponentText(275, 28, "Extreme Donator Commands");
				player.getPackets().sendIComponentText(275, 29, "----------------------------------------");
				player.getPackets().sendIComponentText(275, 30, "::bank");
				
				if (player.getRights() >= 1 && player.isStaff()) {
					player.getPackets().sendIComponentText(275, 32, "Moderator Commands");
					player.getPackets().sendIComponentText(275, 33, "----------------------------------------");
					player.getPackets().sendIComponentText(275, 34, "::mute");
					player.getPackets().sendIComponentText(275, 35, "::kick");
					player.getPackets().sendIComponentText(275, 36, "::jail");
					player.getPackets().sendIComponentText(275, 37, "::sendhome");
					player.getPackets().sendIComponentText(275, 38, "::ticket");
					player.getPackets().sendIComponentText(275, 39, "::finishticket");
					player.getPackets().sendIComponentText(275, 40, "----------------------------------------");
				}
				
				if (player.getRights() == 2 && player.isStaff()) {
					player.getPackets().sendIComponentText(275, 42, "Administrator Commands");
					player.getPackets().sendIComponentText(275, 43, "----------------------------------------");
					player.getPackets().sendIComponentText(275, 44, "::ban");
					player.getPackets().sendIComponentText(275, 45, "::unban");
					player.getPackets().sendIComponentText(275, 46, "::giveitem");
					player.getPackets().sendIComponentText(275, 47, "::promote");
					player.getPackets().sendIComponentText(275, 48, "::demote");
					player.getPackets().sendIComponentText(275, 49, "::tele");
					player.getPackets().sendIComponentText(275, 50, "::unmute");
					player.getPackets().sendIComponentText(275, 51, "::unjail");
					player.getPackets().sendIComponentText(275, 52, "::givespins");
					player.getPackets().sendIComponentText(275, 53, "::item");
					player.getPackets().sendIComponentText(275, 54, "::getid");
					player.getPackets().sendIComponentText(275, 55, "::teleto");
					player.getPackets().sendIComponentText(275, 56, "::update");
					player.getPackets().sendIComponentText(275, 57, "::teletome");
					player.getPackets().sendIComponentText(275, 58, "::god");
					player.getPackets().sendIComponentText(275, 59, "::mypos");
					player.getPackets().sendIComponentText(275, 60, "::givegfx");
					player.getPackets().sendIComponentText(275, 61, "::givedonator");
					player.getPackets().sendIComponentText(275, 62, "::giveexdonator");
					player.getPackets().sendIComponentText(275, 63, "::givedev");
					player.getPackets().sendIComponentText(275, 64, "::takegfx");
					player.getPackets().sendIComponentText(275, 65, "::takedonator");
					player.getPackets().sendIComponentText(275, 66, "::takeexdonator");
					player.getPackets().sendIComponentText(275, 67, "::takedev");
					player.getPackets().sendIComponentText(275, 68, "----------------------------------------");
				}
				return true;
			}
		}
		return false;
	}
}